<?php

namespace App\Http\Controllers\Front;

use App\Model\UserInterPushInfo;
use App\Model\UserInterPushRecordMaterial;
use App\Model\UserInterPushRecord;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UserInterPushOperationController extends Controller
{
    //region 浏览次数的增加
    /**
     * @SWG\Get(
     *     path="/front/material/share",
     *     tags={"互推圈"},
     *     summary="浏览次数的增加",
     *     description="浏览次数的增加",
     *     produces={"application/json"},
     *     @SWG\Parameter(name="record_id", in="path", type="integer", description="分享记录id"),
     *     @SWG\Parameter(name="interpush_id", in="path", type="integer", description="用户互推信息id"),
     *     @SWG\Parameter(name="meterial_id", in="path", type="integer", description="素材id"),
     *      @SWG\Response(
     *          response="200",
     *          description="调用成功！",
     *     )
     * )
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function interPushMaterialShare(Request $request)
    {
        $record_id = $request->input("record_id");
        $interpush_id  = $request->input ("interpush_id");
        $meterial_id = $request->input ("meterial_id");
        $user_interpush_record = UserInterPushRecord::whereId ($record_id)->first();
        if (isset($user_interpush_record)){
            // 被动分享
            $interpush = UserInterPushInfo::whereId($user_interpush_record->interpush_id)->first();
            if($interpush){
                $interpush->increment("others_browse_num");
            }
            $target_interpush = UserInterPushInfo::whereId($user_interpush_record->target_interpush_id)->first();
            if($target_interpush){
                $target_interpush->increment ("forme_brows_num");
            }
            $user_interpush_record_material = UserInterPushRecordMaterial::whereId ($meterial_id)->first();
            if ($user_interpush_record_material){
                $user_interpush_record_material->increment ("browse_num");
            };
        }

        return $this->success();
    }

    //endregion

    //region weChat浏览次数的添加
    public function weChatInterPushMaterialShare(Request $request)
    {
        $created_at = $request->input("created_at");
        $interpush_id  = $request->input ("interpush_id");
        $target_interpush_id = $request->input ("target_interpush_id");
        $interPushInfo = UserInterPushInfo::whereUserId($interpush_id)->first();
        $targetInterPushInfo = UserInterPushInfo::whereUserId ($target_interpush_id)->first();

        $user_interpush_record = UserInterPushRecord::where(["interpush_id" => $interPushInfo->id, "target_interpush_id" =>$targetInterPushInfo->id])->where("created_at", "like", "%".$created_at."%")->first();
        if ($user_interpush_record){
            // 被动分享
            $interpush = UserInterPushInfo::whereId($user_interpush_record->interpush_id)->first();
            $interpush->increment("others_browse_num");
            $target_interpush = UserInterPushInfo::whereId($user_interpush_record->target_interpush_id)->first();
            $target_interpush->increment ("forme_brows_num");
            $user_interpush_record_material = UserInterPushRecordMaterial::whereId ($user_interpush_record->target_meterial_id)->first();
            $user_interpush_record_material->increment ("browse_num");
            return $this->success();
        }
        return $this->success("");
    }
    //endregion
}
